<?php


require_once DIR_FS_ADMIN . 'includes/pachay/domit/xml_domit_parser.php';
require_once DIR_FS_ADMIN . 'includes/pachay/domit/xml_domit_nodemaps.php';
require_once DIR_FS_ADMIN . 'includes/pachay/databases/MyValue.php';


/*
*Author: QuangHuy - 06/06/2008
*Function to get Section Char of type (in querry)
*Parameter $ttype as string, type of field
*Return a Section string
*/

function getSecOfType($tType = 'text')
{
	$mSec = "'";
	switch ($tType) {
		case 'text', 'datetime', 'select', 'multiselect', 'popup', 'radio':
			$mSec = "'";
		break;
		case 'int', 'real', 'checkbox', 'calculation' :
			$mSec = "";
		break;
		
		default:
			$mSec = "'";
		break;
	}
	
}


/*
*Author: QuangHuy - 06/06/2008
*Function to get Array of Column Name of master table
*Parameter $module as string, name of module
*Return a Array none value of MyValue
*/
function getMasterColumnFields($module)
{
	$column_fld = Array();
	
	$xmldoc = new DOMIT_Document();
	$success = $xmldoc->loadXML(DIR_FS_ADMIN."modules/".$module."/pa_module.xml");
	
	if ($success) {
		$nodelist = $xmldoc->selectNodes("/module/master_bean/fields/field");
		for ($i=0; $i < ($nodelist->getLength()); $i++)
		{	
			$node =& $nodelist->item($i);
			
			$mMyValue = new MyValue();
			$mMyValue->setFieldName($node->selectNodes('name',1)->getText());
			$mMyValue->setType(selectNodes('type',1)->getText());
			$mMyValue->setLength((Integer) selectNodes('length',1)->getText());
			$mMyValue->setBeginSec(getSecOfType(selectNodes('type',1)->getText()));
			$mMyValue->setEndSec(getSecOfType(selectNodes('type',1)->getText()));
			$mMyValue->setLabel($node->selectNodes('label',1)->getText());
			
			$column_fld[$node->selectNodes('name',1)->getText()] = $mMyValue;
		}
		
	}
	
	return $column_fld;	
}

/*
Author: QuangHuy - 06/06/2008
*Function to get Array of type Column Value of master table
*Parameter $module as string, name of module
*Return a Array have key is Column Name, value is Type Name of Column
*/
function getMasterColumnTypes($module)
{
	$column_type = Array();
	
	$xmldoc = new DOMIT_Document();
	$success = $xmldoc->loadXML(DIR_FS_ADMIN."modules/".$module."/pa_module.xml");
	if ($success) {
		$nodelist = $xmldoc->selectNodes("/module/master_bean/fields/field");
		for ($i=0; $i < ($nodelist->getLength()); $i++)
		{	
			$node =& $nodelist->item($i);
			$column_type[$node->selectNodes('name',1)->getText()] = $node->selectNodes('type',1)->getText();
		}
		
	}
	
	return $column_type;
}

/*
Author: QuangHuy - 06/06/2008
*Function to get Array of Column Value of master table
*Parameter $module as string, name of module
*Return a Array of MyValue have value, key is name column
*/
//can than khi dung ham nay, phai kiem tra xem $module co dai dien cho module dang duoc focus hay ko
function getMasterColumnValues($module)
{
	$column_fld = Array();
	
	$xmldoc = new DOMIT_Document();
	$success = $xmldoc->loadXML(DIR_FS_ADMIN."modules/".$module."/pa_module.xml");
	
	if ($success) {
		$nodelist = $xmldoc->selectNodes("/module/master_bean/fields/field");
		for ($i=0; $i < ($nodelist->getLength()); $i++)
		{	
			$node =& $nodelist->item($i);
			$name_f = $node->selectNodes('name',1)->getText();
						
			$mMyValue = new MyValue();
			$mMyValue->setFieldName($node->selectNodes('name',1)->getText());
			$mMyValue->setType(selectNodes('type',1)->getText());
			$mMyValue->setLength((Integer) selectNodes('length',1)->getText());
			$mMyValue->setBeginSec(getSecOfType(selectNodes('type',1)->getText()));
			$mMyValue->setEndSec(getSecOfType(selectNodes('type',1)->getText()));
			$mMyValue->setLabel($node->selectNodes('label',1)->getText());
			
			if(isset($_REQUEST[$name_f]))
			{
				$mMyValue->setValue($_REQUEST[$name_f]);
			} else
			{
				$mMyValue->setValue("");
			}
			
			$column_fld[$name_f] = $mMyValue;
			
			
			
		}
		
	}
	
	return $column_fld;	
}


/*
*Author: QuangHuy - 06/06/2008
*Function to get Array of Column Name of sub table
*Parameter $module as string, name of module
*Return a Array none value of MyValue, key is name column
*/
function getSubColumnFields($module)
{
	$column_fld = Array();
	
	$xmldoc = new DOMIT_Document();
	$success = $xmldoc->loadXML(DIR_FS_ADMIN."modules/".$module."/pa_module.xml");
	
	if ($success) {
		$nodelist = $xmldoc->selectNodes("/module/sub_bean/fields/field");
		for ($i=0; $i < ($nodelist->getLength()); $i++)
		{	
			$node =& $nodelist->item($i);
			
			$mMyValue = new MyValue();
			$mMyValue->setFieldName($node->selectNodes('name',1)->getText());
			$mMyValue->setType(selectNodes('type',1)->getText());
			$mMyValue->setLength((Integer) selectNodes('length',1)->getText());
			$mMyValue->setBeginSec(getSecOfType(selectNodes('type',1)->getText()));
			$mMyValue->setEndSec(getSecOfType(selectNodes('type',1)->getText()));
			$mMyValue->setLabel($node->selectNodes('label',1)->getText());
			
			$column_fld[$node->selectNodes('name',1)->getText()] = $mMyValue;
			
			
		}
		
	}
	
	return $column_fld;	
}



/*
Author: QuangHuy - 06/06/2008
*Function to get Array of type Column Value of Sub table
*Parameter $module as string, name of module
*Return a Array have key is Column Name, value is Type Name of Column
*/
function getSubColumnTypes($module)
{
	$column_type = Array();
	
	$xmldoc = new DOMIT_Document();
	$success = $xmldoc->loadXML(DIR_FS_ADMIN."modules/".$module."/pa_module.xml");
	if ($success) {
		$nodelist = $xmldoc->selectNodes("/module/sub_bean/fields/field");
		for ($i=0; $i < ($nodelist->getLength()); $i++)
		{	
			$node =& $nodelist->item($i);
			$column_type[$node->selectNodes('name',1)->getText()] = $node->selectNodes('type',1)->getText();
		}
		
	}
	
	return $column_type;
}



/*
Author: QuangHuy - 06/06/2008
*Function to get Array of Column Value of sub table
*Parameter $module as string, name of module
*Return a Array of Array of MyValue, key is name column
*/
//can than khi dung ham nay, phai kiem tra xem $module co dai dien cho module dang duoc focus hay ko
function getSubColumnValues($module)
{
	$column_fld = Array();
	$count_sub_items = 0;
	if(isset($_REQUEST[$count_items]))  $count_sub_items = $_REQUEST[$count_items];
	
	$xmldoc = new DOMIT_Document();
	$success = $xmldoc->loadXML(DIR_FS_ADMIN."modules/".$module."/pa_module.xml");
	
	if ($success) {
		$nodelist = $xmldoc->selectNodes("/module/sub_bean/fields/field");
		for ($j = 0; $j < $count_sub_items; $j++) 
		{
			$dem = Array();
			for ($i=0; $i < ($nodelist->getLength()); $i++)
			{	
				$node =& $nodelist->item($i);
				$name_f = $node->selectNodes('name',1)->getText();
				
				
				$mMyValue = new MyValue();
				$mMyValue->setFieldName($node->selectNodes('name',1)->getText());
				$mMyValue->setType(selectNodes('type',1)->getText());
				$mMyValue->setLength((Integer) selectNodes('length',1)->getText());
				$mMyValue->setBeginSec(getSecOfType(selectNodes('type',1)->getText()));
				$mMyValue->setEndSec(getSecOfType(selectNodes('type',1)->getText()));
				$mMyValue->setLabel($node->selectNodes('label',1)->getText());
			
				
				if(isset($_REQUEST[$name_f.$j]))
				{
					$mMyValue->setValue = $_REQUEST[$name_f.$j];
				} else
				{
					$mMyValue->setValue = "";
				}
				
				$dem[$name_f] = $mMyValue;
				
			}
			$column_fld[] = $dem;
		}
		
	}
	
	return $column_fld;	
}

/*********************************************************************************************/

/*
Author: QuangHuy - 07/06/2008
*Function to get source Array of field type - select, multiselect
*Parameter  $path as string, addrerd of module spec xml file.
*Parameter $typebean as string, type bean: master_bean or sub_bean
*Parameter $idfield: id of field
*Return a Array of source, key is label source, value is key source. Last element of array have key is "PA_KEY_SELECTED", value is key default
*/

function getSelectSource($path, $typebean, $idfield)
{
	global $paDBO;
	
	$d_source = Array();
	
	$xmldoc = new DOMIT_Document();
	$success = $xmldoc->loadXML($path);
	if ($success) {
		$node = $xmldoc->selectNodes("/module/".$typebean."/fields/field[@id='".$idfield."']",1);
		if ((isset($node))&&(($node->selectNodes('type',1)->getText()=="select"))||($node->selectNodes('type',1)->getText()=="multiselect"))
		{
			
		}
		
		$mSQL = "Select ".$node->selectNodes('key_name',1)->getText().", ".$node->selectNodes('label_name',1)->getText()." From ".$node->selectNodes('table_relate',1)->getText()." ";
		if ($node->selectNodes('where_clause',1)->getText() != "") $mSQL .= (" Where ".$node->selectNodes('where_clause',1)->getText());
		
		
	}
}